package cn.itcast.web.controller;

import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.utils.Md5Utils;
import com.alibaba.druid.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Controller
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private ModuleService moduleService;
    /**
     * 登录跳转
     * @param email 邮箱号
     * @param password 密码
     * @return 返回跳转的页面
     */
    @RequestMapping("login")
//    public String login(String email,String password){
//
//        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)){
//            return "forward:/login.jsp";
//        }
//        User user = userService.findUserByEmail(email);
//        if (user != null && password.equals(user.getPassword())){
//            session.setAttribute("loginUser",user);
//            List<Module> modules = moduleService.findModuleByUserId(user.getId());
//            session.setAttribute("modules",modules);
//            return "/home/main";
//        }else {
//            request.setAttribute("error","用户名或者密码错误");
//            return "forward:/login.jsp";
//        }
//    }
    public String login(String email,String password){
        try {
            //获取subject
            Subject subject = SecurityUtils.getSubject();
            //构造用户名和密码
            UsernamePasswordToken upToken = new UsernamePasswordToken(email, password);
            //借助subject完成登录
            subject.login(upToken);
            //通过shiro获取用户对象
            User user = (User) subject.getPrincipal();
            session.setAttribute("loginUser",user);
            List<Module> modules = moduleService.findModuleByUserId(user.getId());
            session.setAttribute("modules",modules);
            return "/home/main";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            //登录失败跳转页面
            request.setAttribute("error","用户名或者密码错误");
            return "forward:login.jsp";
        }
    }
    @RequestMapping("logout")
    public String logout(){
        // shiro提供了退出方法(清除shiro的认证信息)
        SecurityUtils.getSubject().logout();
        //得到会话
        request.getSession();
        //会话失效
        //session.invalidate();
        //重定向到登录页面
        return "forward:/login.jsp";
    }

    @RequestMapping("home")
    public String home(){
        return "/home/home";
    }


    @RequestMapping("checkEmail")
    @ResponseBody
    public boolean checkEmail(String email) throws UnsupportedEncodingException {
        User user = userService.findUserByEmail(email);
        if (user != null){
            return true;
        }
        return false;
    }

}
